#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   killall_tcp_traffic
#
# Description:
#   Kill all of the tcp traffic utilities (ns-tcpserver, ns-tcpclient)
#
# Arguments:
#   None
#
# Returns:
#   None
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT

# Check the environmanet variable for the test
. check_envval || exit 1

# Waiting time before outputting a warning message [sec]
WARN_WAIT=300


# Send SIGHUP both server and client
killall -SIGHUP ns-tcpserver >/dev/null 2>&1
$LTP_RSH $RHOST "killall -SIGHUP ns-tcpclient" >/dev/null 2>&1

# Verify the server is dead.
start_epoc=`date +%s`
while true ; do
    ps auxw | fgrep -v grep | fgrep -l ns-tcpserver >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	break
    fi

    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`
    if [ $elapse_epoc -ge $WARN_WAIT ]; then
	echo "TCP traffic server is not dead over $WARN_WAIT sec" >&2
    fi

    killall -SIGHUP ns-tcpserver >/dev/null 2>&1
    sleep 1
done

# Verify the client is dead.
start_epoc=`date +%s`
while true ; do
    ##ret=`$LTP_RSH $RHOST 'ps auxw | fgrep -v grep | grep -l '[[:blank:]]ns-tcpclient[[:blank:]]' >/dev/null 2>&1; echo $?'`
    ret=`$LTP_RSH $RHOST 'ps auxw | fgrep -v grep | grep -l '/ns-tcpclient[[:blank:]]' >/dev/null 2>&1; echo $?'`

    if [ -z $ret ]; then
	continue
    fi

    if [ $ret -ne 0 ]; then
	break
    fi

    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`
    if [ $elapse_epoc -ge $WARN_WAIT ]; then
	echo "TCP traffic client is not dead over $WARN_WAIT sec" >&2
    fi

    $LTP_RSH $RHOST "killall -SIGHUP ns-tcpclient" >/dev/null 2>&1
    sleep 1
done
